#include "Multiplier.h"

Multiplier::Multiplier(Matrix & left, Matrix & right, vector<float> * &values, vector<int> * &rows, int * columnIndexes)
{
	this->left = left;
	this->right = right;
	this->values = values;
	this->rows = rows;
	this->columnIndexes = columnIndexes;
}

void Multiplier::operator()(const blocked_range<int> & range) const
{
	int size = left.Size;		
	int * temp = new int[size];

	int i, j, k;

	for (i = range.begin(); i < range.end(); i++)
	{
		memset(temp, -1, size * sizeof(int));

		for (j = right.ColumnIndexes[i]; j < right.ColumnIndexes[i + 1]; j++)
		{
			int row = right.Rows[j];
			temp[row] = j;
		}

		for (j = 0; j < size; j++)
		{
			float sum = 0;

			for (k = left.ColumnIndexes[j]; k < left.ColumnIndexes[j + 1]; k++)				
			{
				int row = left.Rows[k];
				int index = temp[row];

				if (index != -1)
				{
					sum += left.Values[k] * right.Values[index];
				}					
			}

			if (sum != 0)
			{
				rows[i].push_back(j);
				values[i].push_back(sum);
				columnIndexes[i]++;
			}
		}
	}

	delete[] temp;
}